package chat

import (
	"github.com/gin-gonic/gin"
	"go-api-template/api/http"
	"go-api-template/api/ws/front"
	"go-api-template/service"
)

type SendMessageReq struct {
	Msg string `form:"msg"`
}

type SendMessageResp struct {
	Reply string `json:"reply"`
}

func (req *SendMessageReq) Request(ctx *gin.Context, s *service.Service) (http.Resp, error) {
	return req.Do(ctx, s)
}

func (req *SendMessageReq) Do(_ *gin.Context, s *service.Service) (resp http.Resp, err error) {
	msg, err := GPTCompletions(req.Msg)

	if err == nil {
		front.HUB.OnlineMsgPool <- front.Message{Data: msg}
	} else {
		front.HUB.OnlineMsgPool <- front.Message{Data: err.Error()}
	}

	resp.SetData(SendMessageResp{Reply: msg})
	return
}
